<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<script type="text/javascript">
function loadMenu(){
	$.ajax({
	 	url: "viewAllMenuJson.do", 
		type: 'POST', 
		dataType: 'json', 
		timeout: 25000, 
		error: function(){alert('Error ');}, 
		success: function(result){
			var menuContent="<ul class='nav nav-list' id='menu_ul'>";
			$.each(result,function(idx,item){    
				var ptext=""; 
			   	if(item.menupid==0){
			   		ptext=ptext+"<li name='menu_li_1'><a  href='"+item.pageurl+"' class='dropdown-toggle'><i class='icon-desktop'></i><span class='menu-text'>"+item.name+"</span><b class='arrow icon-angle-down'></b></a>";
			   		ptext=ptext+"<ul class='submenu'>";
			   		ptext=ptext+getChild(result,item);
			   		ptext=ptext+"</ul>";
			   		ptext=ptext+"</li>";
			   	}
			   	menuContent=menuContent+ptext;
			});
			menuContent=menuContent+"</ul>";
			$("#my_menu").html(menuContent);
			
			
			//绑定菜单中div中li元素点击事件加class
			$('li[name="menu_li_2"]').bind("click", function () {
				//二级节点
    			$('li[name="menu_li_2"]').removeClass("active");
    			$(this).attr("class", "active");
    			//一级节点
    			$('li[name="menu_li_1"]').removeClass("active");
    			$(this).parent().parent().addClass("active");
    			//路径赋值
    			var homePath="<li><i class='icon-home home-icon'></i><a href='Javascript:void(0)'>主页</a></li>";
    			var path1="<li><a href='Javascript:void(0)'>"+$(this).parent().parent().find("span").html()+"</a></li>";
				var path2="<li class='active'>"+$(this).find("span").html()+"</li>";
    			$("#menu_path").html(homePath+path1+path2);
			});
			
			$('li[name="menu_li_1"]').bind("click", function () {
				//$('.submenu')
				//$(this).slibings('li').removeClass("active").removeClass("open");
    			$('li[name="menu_li_1"]').removeClass("active");
    			//$(this).addClass("active");
    			//$(this).addClass("open");
			});
		} 
	});
}
//获取所有子节点
function getChild(json,pitem){
	var ptext="";
	$.each(json,function(idx,item){
		if(pitem.id==item.menupid&&item.type==1){
			ptext=ptext+"<li name='menu_li_2'><a href='<%=request.getContextPath()%>"+item.pageurl+"' target='mainiframe'><i class='icon-double-angle-right'></i><span class='menu-text'>"+item.name+"</span></a>";
			ptext=ptext+getChild(json,item);
			ptext=ptext+"</li>";
		}
	});
	return ptext;
}

</script>
<div id="menu_div">
	<ul class="nav nav-list" id="my_menu">
	</ul>
</div>

<!-- load menu -->
<script type="text/javascript">
	window.onload = loadMenu;
</script>
